//
//  CheckInformationController.m
//  Estate
//
//  Created by heyong on 16/6/22.
//  Copyright © 2016年 ttsx. All rights reserved.
//

#import "CheckInformationController.h"
#import "DefaineCell.h"
#import "B_wuLiaoCell.h"
@interface CheckInformationController ()
@property (strong, nonatomic)NSMutableArray *titleForSec1;
@property (strong, nonatomic)NSMutableArray *dataArr;
@property (strong, nonatomic) FMDatabase *db;
@end

@implementation CheckInformationController

- (FMDatabase *)db{
    if(_db==nil){
        _db=[FMDatabase databaseWithPath:DEF_DB_DATABASEPATH];
    }
    return  _db;
}
-(NSMutableArray *)dataArr{
    
    if(_dataArr==nil){
        _dataArr=[NSMutableArray array];
    }
    return _dataArr;
}

- (NSMutableArray *)titleForSec1{
    if(_titleForSec1==nil){
        _titleForSec1=[NSMutableArray arrayWithObjects:@"单位名称:",@"试验员:",nil];
    }
    return _titleForSec1;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.bounces=NO;
    //注册单元格
    [self.tableView registerNib:[UINib nibWithNibName:@"DefaineCell" bundle:nil] forCellReuseIdentifier:@"deCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"B_wuLiaoCell" bundle:nil] forCellReuseIdentifier:@"textCell"];
    self.tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    self.tableView.tableFooterView=[[UIView alloc]initWithFrame:CGRectZero];
    [self initData];
}

- (void)initData{
    NSString *sqlStr=[NSString stringWithFormat:@"SELECT ID,PRO_UNIT_NAME,PRO_UNIT_PHONE,PRO_UNIT_ADDRESS,PRO_UNIT_EMAIL FROM PROJECT_UNIT WHERE PRO_ID='%@' and PRO_UNIT_TYPE='UNIT_03'",PROID];
    [self.db open];
    FMResultSet *result=[self.db executeQuery:sqlStr];
    while ([result next]) {
        NSMutableDictionary *dic=[NSMutableDictionary dictionary];
        //单位名
        [dic setObject:[result stringForColumn:@"PRO_UNIT_NAME"] forKey:@"name"];
        
        NSString *unitId=[result stringForColumn:@"ID"];
        NSString *sql_2=[NSString stringWithFormat:@"SELECT USER_ID,USER_TYPE FROM PROJECT_USER WHERE UNIT_ID='%@' AND USER_TYPE='CONSTRACT_08'",unitId];
        FMResultSet *result_2=[self.db executeQuery:sql_2];
        //实验员名字数组
        NSMutableArray *sNameArr=[NSMutableArray array];
        while ([result_2 next]) {
            NSString *userID=[result_2 stringForColumn:@"USER_ID"];
            NSString *name=[LSUtility getuserIdByUserName:userID][@"REAL_NAME"];
            [sNameArr addObject:name];
        }
        NSString *names=@"";
        for (NSString *name in sNameArr) {
            if([names isEqualToString:@""]){
                names=[names stringByAppendingString:[NSString stringWithFormat:@"%@",name]];
            }else{
                names=[names stringByAppendingString:[NSString stringWithFormat:@"%@,",name]];

            }
        }
        [dic setObject:names forKey:@"syy"];

        //实验员
        [self.dataArr addObject:dic];
    }
    [self.db close];
}


#pragma mark -UITableViewDataSource

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArr.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if(section==0){
        return 0;
    }
    return 20;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 2;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.row==1){
        return 120;
    }
    return 44;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIImageView *img=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, 8)];
    img.backgroundColor=[UIColor whiteColor];
    img.image=[UIImage imageNamed:@"juchishang"];
    return img;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 8;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    long row=indexPath.row;
    long section=indexPath.section;
    NSDictionary *dic=self.dataArr[section];
    if(row==1){
        B_wuLiaoCell *cell=[tableView dequeueReusableCellWithIdentifier:@"textCell" forIndexPath:indexPath];
        cell.isEdit=NO;
        cell.title.text=@"试验员:";
        cell.text.text=dic[@"syy"];
        return cell;
    }
    
    DefaineCell *cell=[tableView dequeueReusableCellWithIdentifier:@"deCell" forIndexPath:indexPath];
    [cell setTitle:self.titleForSec1[row] andDetail:dic[@"name"] andLeftConstraint:5];
    return cell;
}

/*
 *  此方法让cell横线置顶
 */
-(void)viewDidLayoutSubviews {
    
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsZero];
        
    }
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)])  {
        [self.tableView setLayoutMargins:UIEdgeInsetsZero];
    }
    
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPat{
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]){
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    
}
@end
